<?php
/**
 * Created by PhpStorm.
 * User: 76683
 * Date: 2018/7/16
 * Time: 23:59
 */

namespace app\api\model;


use think\Model;

class WeDayTopic extends Model
{
    /**
     * 今日话题
     * @param string $uid
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function todayTopic($uid = '0')
    {
        $map = [];
        if($uid){
            $field = 't.id,u.nick_name,u.we_nick_name,u.reset_name,t.likes,t.content,t.photos,t.details,t.create_time, case l.user_id when l.user_id then 1 else 0 end as is_liked,case td.user_id when td.user_id then 1 else 0 end as is_detailed';
        }else{
            $field = 't.id,t.likes,t.content,t.photos,t.details,t.create_time';
        }
        $today = strtotime(date('Ymd 0:00:00', time()));
        $tomorrow = strtotime(date('Ymd 0:00:00', strtotime('+1 day')));
        $data = $this->alias('tt')
            ->join('__WE_TOPIC__ t', 't.id = tt.topic_id and t.is_delete = 0 and t.status = 1 and tt.is_delete = 0')
            ->join('__WE_LIKES__ l', 't.id = l.topic_id', 'left')
            ->join('__WE_TOPIC_DETAIL__ td','td.topic_id = t.id and td.is_delete = \'0\' and td.user_id = \''.$uid.'\'','left')
            ->join('__WE_USERS__ u','u.id = t.user_id')
            ->where(array_merge(['tt.show_date' => [['>=', $today], ['<', $tomorrow], 'and']],$map))
            ->field($field)
            ->fetchSql(false)
            ->find();
        return empty($data) ? [] : $data->toArray();
    }

    /**
     * 图片获取器
     * @param $value
     * @return array
     */
    public function getPhotosAttr($value){
        return array_map(function ($item){
            return config('web_site_domain').$item;
        },get_files_path(explode(',',$value)));
    }

    /**
     * 格式化创建时间
     * @param $value
     * @return false|string
     */
    public function getCreateTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
}